package com.su.lightthings.fragment.device

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.su.lightthings.adapter.WatchFunctionAdapter
import com.su.lightthings.base.BaseFragment
import com.su.lightthings.bean.WatchFunctionBean
import com.su.lightthings.databinding.FragmentWatchFunctionBinding
import com.su.lightthings.utils.Cons

class WatchFunctionFragment : BaseFragment<FragmentWatchFunctionBinding>() {

    private val mAdapter: WatchFunctionAdapter by lazy {
        WatchFunctionAdapter()
    }
    private var index: Int = -1

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    }

    override fun getViewBinding(inflater: LayoutInflater, container: ViewGroup?): FragmentWatchFunctionBinding =
        FragmentWatchFunctionBinding.inflate(layoutInflater)

    override fun initViews() {
        index = arguments?.getInt("index") ?: -1
        Log.d("TAG", "onViewCreated: $index")

        binding.tvTitle.text = getTitle(index)
        binding.ivBack.setOnClickListener {
            findNavController().popBackStack()
        }

        binding.rv.apply {
            adapter = mAdapter
            layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
            addItemDecoration(
                DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL)
            )
        }
        mAdapter.submitList(getFunctionList(index))
    }

    private fun getTitle(index: Int): String {
        return when (index) {
            0 -> "手表设置"
            1 -> "健康数据设置"
            2 -> "其他功能设置"
            else -> "--"
        }
    }

    private fun getFunctionList(index: Int): MutableList<WatchFunctionBean> {
        return when (index) {
            0 -> Cons.FunctionItemList.watchList
            1 -> Cons.FunctionItemList.healthList
            2 -> Cons.FunctionItemList.otherList
            else -> arrayListOf()
        }
    }

}